package com.atguigu.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrdersettingMapper;
import com.atguigu.pojo.Ordersetting;
import com.atguigu.pojo.OrdersettingExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    public void save(List<Ordersetting> ordersettings) {
        for (Ordersetting ordersetting : ordersettings) {
            //设置查询条件
            OrdersettingExample ordersettingExample = new OrdersettingExample();
            OrdersettingExample.Criteria criteria = ordersettingExample.createCriteria();
            criteria.andOrderdateEqualTo(ordersetting.getOrderdate());
            long count = ordersettingMapper.countByExample(ordersettingExample);
            //如果在ordersetting表中存在指定日期的数据  更新
            if (count > 0){
                ordersettingMapper.updateByExampleSelective(ordersetting,ordersettingExample);
            }else {//不存在  添加
                ordersettingMapper.insert(ordersetting);
            }
        }
    }

    @Override
    public List<Map<String, Integer>> query(String date) {

        List<Map<String,Object>> list=ordersettingMapper.queryByDate(date+"-%");
        List<Map<String, Integer>> arrayList = new ArrayList<Map<String, Integer>>();

        for (Map<String, Object> map : list) {
            Date date1 = (Date) map.get("orderDate");
            int date2 = date1.getDate();
            Integer number = Integer.parseInt(map.get("number").toString());
            Integer reservations = (Integer) map.get("reservations");
            if(reservations== null){
                reservations=0;
            }

            HashMap<String, Integer> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("date",date2);
            stringObjectHashMap.put("number",number);
            stringObjectHashMap.put("reservations",reservations);
            arrayList.add(stringObjectHashMap);
        }

        return arrayList;
    }

    @Override
    public void update(Map<String, Object> map) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(map.get("date1").toString());
        Ordersetting ordersetting =new Ordersetting(date,Integer.parseInt(map.get("number").toString()));
        OrdersettingExample orderSettingExample = new OrdersettingExample();
        OrdersettingExample.Criteria criteria = orderSettingExample.createCriteria();
        criteria.andOrderdateEqualTo(date);


        long count = ordersettingMapper.countByExample(orderSettingExample);
        if(count >0){
            ordersettingMapper.updateByExampleSelective(ordersetting,orderSettingExample);
        }else {
            ordersettingMapper.insert(ordersetting);
        }
    }
}
